package com.fruit.service.goodspackage.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.goodspackage.GoodsPackageMapper;
import com.fruit.mapper.goodspackage.detail.GoodsPackageDetailMapper;
import com.fruit.service.goodspackage.GoodsPackageManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 产品套餐相关服务
 * 
 * @author Administrator
 * @version 2018.02.02 11:40
 */
@Slf4j
@Service("goodsPackageService")
public class GoodsPackageService implements GoodsPackageManager {
	@Autowired
	private GoodsPackageMapper goodsPackageMapper;
	@Autowired
	private GoodsPackageDetailMapper goodsPackageDetailMapper;

	@Override
	public Map<String, Object> getRecords(Map<String, Object> params) {
		log.info("【产品套餐列表服务】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		if (params == null) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数不能为null!"));
			return result;
		}
		log.info("【产品套餐列表服务】---【params】---{}", params.toString());
		if (params.get("startNum") == null || "".equals(params.get("startNum"))) { // 起始记录数
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入起始记录数不能为空!"));
			return result;
		}
		if (params.get("pageNum") == null || "".equals(params.get("pageNum"))) { // 每页显示数
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入每页显示数不能为空!"));
			return result;
		}
		try {
			// 查询产品套餐列表
			List<Map<String, Object>> list = goodsPackageMapper.getRecords(params);
			result.put("data", list);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			log.error("【产品套餐列表服务】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【产品套餐列表服务】---【结束】---");
		return result;
	}

	@Override
	public Map<String, Object> selectRecordByKey(Long id) {
		log.info("【产品套餐详情服务】---【开始】---");
		Map<String, Object> result = new HashMap<String, Object>();
		if (id == null) {
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "传入参数id不能为空!"));
			return result;
		}
		log.info("【产品套餐详情服务】---【id】---{}", id);
		try {
			// 查询产品套餐详情
			Map<String, Object> list = goodsPackageMapper.selectRecordByKey(id);
			log.info("【产品套餐详情服务】---【查询成功】---");
			Integer num = goodsPackageDetailMapper.getGoodsPackageDetailNum(id);
			result.put("data", list);
			result.put("goodsNum", num);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", ""));
		} catch (Exception e) {
			log.error("【产品套餐详情服务】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常"));
		}
		log.info("【产品套餐详情服务】---【结束】---");
		return result;
	}

}
